package com.uchengwang.ui.cm_ui.umsg.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.chanven.lib.cptr.PtrClassicFrameLayout;
import com.chanven.lib.cptr.PtrDefaultHandler;
import com.chanven.lib.cptr.PtrFrameLayout;
import com.chanven.lib.cptr.loadmore.OnLoadMoreListener;
import com.chanven.lib.cptr.recyclerview.RecyclerAdapterWithHF;
import com.uchengwang.R;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.http.listener.OnHttpResultListener;
import com.uchengwang.ui.UChengApplication;
import com.uchengwang.ui.base_ui.ui.BaseWithTitleActivity;
import com.uchengwang.ui.cm_ui.me.ui.CreateGroupActivity;
import com.uchengwang.ui.cm_ui.cycle.bean.RyGroupBean;
import com.uchengwang.ui.cm_ui.rychat.manager.RyGroupManager;
import com.uchengwang.ui.cm_ui.umsg.adapter.GroupAdapter;
import com.uchengwang.ui.utils.UCommonUtil;

import java.util.List;

/**
 * Created by U城网 on 2017/10/13.
 * 老乡会
 */

public class SameAddrActivity extends BaseWithTitleActivity{
    private RecyclerView group_rv;
    private TextView fm_group_noNet;
    private PtrClassicFrameLayout mSwipeRefreshLayout;
    private RecyclerAdapterWithHF mAdapter;
    private GroupAdapter adapter;
    private long orgId;//某个校吧ID
    private int orgType;//组织类别(0=社团 1=兴趣圈 2=老乡会)
    private int page =1, totalPage;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.fragment_group_layou);
        orgId = getIntent().getLongExtra(BaseConfig.ORG_ID, -1);
        orgType = getIntent().getIntExtra(BaseConfig.ORG_TYPE, -1);
        setTitle("老乡群");

        if (UCommonUtil.getUtil().isLogin()){
            SameAddrActivity.this.create();

//            if (WhoCanMoreGroupConfig.getConfig().isCanMore(UCommonUtil.getUtil().getId())){
//                //可以创建更多的群
//                SameAddrActivity.this.create();
//            }else {
//                if (UChengApplication.userRole == BaseConfig.STUDENT_ROLE){
//                    //学生用户才可以创建校吧交流群
//                    RyGroupManager.getManager().isCreateGroupInBa(this, orgType, new ClubHttpManager.CreateOrNotListener() {
//                        @Override
//                        public void create(int stutas, boolean isCreate) {
//                            if ((stutas == 0 || stutas ==1) && !isCreate){
//                                SameAddrActivity.this.create();
//                            }
//                        }
//                    });
//                }
//            }
        }


        initView();
    }

    private void initView() {
        group_rv = (RecyclerView) findViewById(R.id.fm_group_rv);
        fm_group_noNet = (TextView) findViewById(R.id.fm_group_noNet);
        mSwipeRefreshLayout = (PtrClassicFrameLayout) findViewById(R.id.fm_group_layout);
        initData();
    }

    private void initData() {
        LinearLayoutManager manager = new LinearLayoutManager(this);
        adapter = new GroupAdapter(this);
        mAdapter = new RecyclerAdapterWithHF(adapter);
        group_rv.setLayoutManager(manager);
        group_rv.setAdapter(mAdapter);

        mSwipeRefreshLayout.setAutoLoadMoreEnable(true);
        mSwipeRefreshLayout.setLoadMoreEnable(true);

        mSwipeRefreshLayout.postDelayed(new Runnable() {
            @Override
            public void run() {
                mSwipeRefreshLayout.autoRefresh(true);
            }
        }, 150);

        mSwipeRefreshLayout.setPtrHandler(new PtrDefaultHandler() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                page = 1;
                getMyGroup();
                getBaGroup(true);
            }
        });

        mSwipeRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {

            @Override
            public void loadMore() {
                page++;
                if (page <= totalPage){
                    //当前请求页大于总页数，不再请求
                    getBaGroup(false);
                } else {
                    mSwipeRefreshLayout.loadMoreComplete(false);
                }
            }
        });
    }

    /**
     * 获取老乡会的群组
     * */
    private void getBaGroup(final boolean isRefresh){
        RyGroupManager.getManager().getGroups(this, 2, page, 10, BaseConfig.GROUP_LX, 0,UChengApplication.uniId, "", new OnHttpResultListener<RyGroupBean>() {
            @Override
            public void onResult(int status, String msg, int total, List<RyGroupBean> list) {
                if (isRefresh){
                    mSwipeRefreshLayout.refreshComplete();
                }else {
                    mSwipeRefreshLayout.loadMoreComplete(true);
                }
                if (status ==1){
                    adapter.getData().clear();
                    adapter.notifyDataSetChanged();
                    return;
                }

                if (list != null && list.size() >0){
                    totalPage = total;
                    if (isRefresh){
                        adapter.setData(list);
                    }else {
                        adapter.getData().addAll(list);
                        adapter.notifyDataSetChanged();
                    }
                }


            }
        });
    }

    /**
     * 获取我的群组信息
     * */
    private void getMyGroup(){
//        RyGroupManager.getManager().getMyGroups(this, 1, 999, 0, new OnHttpResultListener<RyGroupBean>() {
//            @Override
//            public void onResult(int status, String msg, int total, List<RyGroupBean> list) {
//                if (list != null && list.size() >0){
////                    adapter.setGroups(list);
//                }
//            }
//        });
    }

    private void create(){
        setRightText("创建", new View.OnClickListener() {//建群暂时取消
            @Override
            public void onClick(View view) {
                //创建交流群
                if (UCommonUtil.getUtil().isNeedLogin(SameAddrActivity.this, false)){
                    return;
                }
                Intent intent = new Intent(SameAddrActivity.this, CreateGroupActivity.class);
                intent.putExtra(BaseConfig.TYPE, 3);
                intent.putExtra(BaseConfig.ORG_TYPE, orgType);
                intent.putExtra(BaseConfig.PUB_ID, orgId);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (BaseConfig.isRelease){
            mSwipeRefreshLayout.postDelayed(new Runnable() {
                @Override
                public void run() {
                    mSwipeRefreshLayout.autoRefresh(true);
                }
            }, 150);
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        BaseConfig.isRelease = false;
    }

}
